using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Jasongozy.Copor;
using Jasongozy.Copor.Utility;
using Jasongozy.Alaton;
namespace Copor.Test.UnitTest {
    [TestFixture]
    public class TemplateNodeParserTest {
        [Test]
        public void ParseTest() {
            TemplateNodeDefaultParser parser = new TemplateNodeDefaultParser();
            TemplateTreeNode node = parser.Parse(@"D:\PersonalProject\2.0\Copor.Test\TestData\CS\MySql\SqlProc\MainTemplate.xml");
            Assert.AreEqual(true, node.HasChild);
        }

        [Test]
        public void SyntaxTest() {
            TemplateNodeDefaultParser parser = new TemplateNodeDefaultParser();
            TemplateTreeNode node = parser.Parse(@"D:\PersonalProject\2.0\Copor.Test\TestData\CS\MySql\SqlProc\MainTemplate.xml");
            
            string connect = "Server=10.2.6.47;uid=DMClient;pwd=2Fetch(Get;allow zero datetime=no;pooling=true;charset=utf8;database=PeopleData;";
            string dbname = "PeopleData";
            TableSchema schema = MySqlDBHelper.GetTableSchema(connect, dbname, "CategoryAverageReturn");
            TemplateParameter.InitTemplateParameter(schema, connect, dbname,"Csharp", "mysql", "sqltxt");
            Analysis(node);
            Assert.AreEqual(true, node.HasChild);
            Console.WriteLine(DisplayTemplateTreeNode(node));
        }

        [Test]
        public void SyntaxAndInjectionTest() {
            TemplateNodeDefaultParser parser = new TemplateNodeDefaultParser();
            TemplateTreeNode node = parser.Parse(@"D:\PersonalProject\2.0\Copor.Test\TestData\CS\MySql\SqlProc\MainTemplate.xml");

            string connect = "Server=10.2.6.47;uid=DMClient;pwd=2Fetch(Get;allow zero datetime=no;pooling=true;charset=utf8;database=PeopleData;";
            string dbname = "PeopleData";
            TableSchema schema = MySqlDBHelper.GetTableSchema(connect, dbname, "CategoryAverageReturn");
            TemplateParameter.InitTemplateParameter(schema, connect, dbname, "Csharp", "mysql", "sqltxt");
            Analysis(node);
            Injection(node);
            Assert.AreEqual(true, node.HasChild);
            Console.WriteLine(DisplayTemplateTreeNode(node));
        }


        private static void Analysis(TemplateTreeNode node) {
            TemplateSyntax syn = new TemplateSyntax();
            if (!node.HasChild) syn.Analysis(node);
            else {
                foreach (TemplateTreeNode n in node.ChildNodes) {
                    Analysis(n);
                }
            }
        }

        private static void Injection(TemplateTreeNode node) {
            if (!node.HasChild) node.TemplateContent = TemplateParamInjection.Injection(node.TemplateContent);
            else {
                foreach (TemplateTreeNode n in node.ChildNodes) {
                    Injection(n);
                }
            }
        }

        private string DisplayTemplateTreeNode(TemplateTreeNode node) {
            StringBuilder code = new StringBuilder();
            code.AppendLine(node.TemplateContent);
            foreach (TemplateTreeNode subNode in node.ChildNodes) {
                code.AppendLine(DisplayTemplateTreeNode(subNode));
            }
            return code.ToString();
        }

        private TableSchema InitMockSchema() {
            TableSchema schema = new TableSchema();
            schema.TableName = "table1";
            schema.Pks = new string[] { "c1", "c2" };
            schema.Columns = new string[] { "c1", "c2", "c3" };
            schema.ColumnTypes = new string[] { "System.String", "System.String", "System.Int32" };
            schema.DefaultValues = new Dictionary<string, string>();
            schema.DefaultValues.Add("c1", "xxxx1");
            return schema;
        }
    }
}
